package com.ruoyi.know.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.know.domain.PointRecords;
import com.ruoyi.know.service.IPointRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分记录Controller
 *
 * @author ruoyi
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/know/records")
public class PointRecordsController extends BaseController
{
    @Autowired
    private IPointRecordsService pointRecordsService;

    /**
     * 查询积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('know:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(PointRecords pointRecords)
    {
        startPage();
        List<PointRecords> list = pointRecordsService.selectPointRecordsList(pointRecords);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('know:records:totalPoints')")
    @GetMapping(value = "/totalPoints/{userId}")
    public AjaxResult totalPoints(@PathVariable("userId") Long userId)
    {
        Integer totalPoints = pointRecordsService.selectTotalPoints(userId);
        if(totalPoints != null){
            return success(totalPoints);
        }else{
            return error("获取积分信息错误");
        }
    }
    /**
     * 导出积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('know:records:export')")
    @Log(title = "积分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointRecords pointRecords)
    {
        List<PointRecords> list = pointRecordsService.selectPointRecordsList(pointRecords);
        ExcelUtil<PointRecords> util = new ExcelUtil<PointRecords>(PointRecords.class);
        util.exportExcel(response, list, "积分记录数据");
    }

    /**
     * 获取积分记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:records:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointRecordsService.selectPointRecordsById(id));
    }

    /**
     * 新增积分记录
     */
    @PreAuthorize("@ss.hasPermi('know:records:add')")
    @Log(title = "积分记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointRecords pointRecords)
    {
        return toAjax(pointRecordsService.insertPointRecords(pointRecords));
    }

    /**
     * 修改积分记录
     */
    @PreAuthorize("@ss.hasPermi('know:records:edit')")
    @Log(title = "积分记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointRecords pointRecords)
    {
        return toAjax(pointRecordsService.updatePointRecords(pointRecords));
    }

    /**
     * 删除积分记录
     */
    @PreAuthorize("@ss.hasPermi('know:records:remove')")
    @Log(title = "积分记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointRecordsService.deletePointRecordsByIds(ids));
    }
}
